﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utils;
using Maticsoft.DBUtility;

public partial class ajax_car : BasePage

{
    // Fields
    public List<Maticsoft.Model.Car> carlist = new List<Maticsoft.Model.Car>();
    public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
    public List<Maticsoft.Model.Order> orderlist = new List<Maticsoft.Model.Order>();
    public Maticsoft.BLL.Team teambll = new Maticsoft.BLL.Team();
    public Maticsoft.Model.Team teammodel = new Maticsoft.Model.Team();
    public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();

    // Methods
    public void AddCar(string teamid)
    {
        this.Judge(Convert.ToInt32(teamid));
        this.teammodel = this.teambll.GetModel(Convert.ToInt32(teamid));
        if ((((this.teammodel.Farefree == 0) || (this.teammodel.Delivery == "coupon")) || (this.teammodel.Card > 0)) || (this.teammodel.Team_type != "normal"))
        {
            base.Response.Redirect("../team/buy.aspx?id=" + this.teammodel.Id);
        }
        else
        {
            string fee = "";
            fee = this.teammodel.Fare.ToString();
            CookieCar.AddProductToCar(teamid, "1", "", this.teammodel.Team_price, this.teammodel.Image, this.teammodel.Per_number.ToString(), this.teammodel.Farefree.ToString(), fee);
            base.Response.Redirect("../shopcart/show.aspx");
        }
    }

    public void delete(string teamid)
    {
        CookieCar.DeleteProduct(teamid);
        base.Response.Redirect("../shopcart/show.aspx");
    }

    public decimal GetFarfee(string teamid, int n, string f)
    {
        decimal num = 0M;
        int num2 = 0;
        try
        {
            this.carlist = CookieCar.GetCarData();
            decimal[] m = new decimal[this.carlist.Count];
            for (int i = 0; i < this.carlist.Count; i++)
            {
                if (this.carlist[i].Qid == teamid)
                {
                    num2 += n;
                    m[i] = decimal.Parse(f);
                }
                else
                {
                    m[i] = decimal.Parse(this.carlist[i].Fee);
                    num2 += Convert.ToInt32(CookieCar.GetProductInfo(this.carlist[i].Qid).Split(new char[] { ',' })[1]);
                }
            }
            m = Utility.sort(m);
            num = m[0];
            int num3 = Convert.ToInt32(CookieCar.GetProid(num.ToString()));
            if (num2 >= num3)
            {
                return 0M;
            }
            num = m[0];
        }
        catch (Exception)
        {
        }
        return num;
    }

    public void Judge(int teamid)
    {
        int @int = Helper.GetInt(DbHelperSQL.Query(string.Concat(new object[] { "select count(*) from [orderdetail] inner join [order] on(orderdetail.order_id=[order].id) where [order].user_id=", base.ASUser.Id, " and teamid=", teamid, " and state='pay'" })).Tables[0].Rows[0][0], 0);
        int num2 = Helper.GetInt(DbHelperSQL.Query(string.Concat(new object[] { "select count(*) from [order] where user_id=", base.ASUser.Id, " and team_id=", teamid, " and state='pay'" })).Tables[0].Rows[0][0], 0);
        this.teammodel = this.teambll.GetModel(teamid);
        if (this.teammodel == null)
        {
            this.SetError("没有该项目");
            base.Response.Redirect("/index.aspx");
            base.Response.End();
        }
        else if ((this.teammodel.Buyonce == "Y") && ((@int + num2) > 0))
        {
            this.SetError("您已经购买过此项目，当前项目只允许购买一次");
            base.Response.Redirect("~/team.aspx?id=" + teamid);
            base.Response.End();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {
            if (!(this.Page.User.Identity.IsAuthenticated && !(this.Page.User.Identity.Name == "")))
            {
                base.SetRefer(base.Request.Url.AbsoluteUri.ToString());
                base.Response.Redirect("~/account/Login.aspx");
            }
            if (base.Request["id"] != null)
            {
                this.AddCar(base.Request["id"].ToString());
            }
            if (base.Request["delid"] != null)
            {
                this.delete(base.Request["delid"].ToString());
            }
            if ((base.Request["proid"] != null) && (base.Request["num"] != null))
            {
                this.update(base.Request["proid"].ToString(), Convert.ToInt32(base.Request["num"]));
            }
        }
    }

    public void update(string teamid, int num)
    {
        this.teammodel = this.teambll.GetModel(Convert.ToInt32(teamid));
        string fee = "fee";
        fee = this.teammodel.Fare.ToString();
        CookieCar.UpdateQuantity(teamid, num, "", this.teammodel.Team_price, this.teammodel.Image, this.teammodel.Per_number.ToString(), this.teammodel.Farefree.ToString(), fee);
        base.Response.Write(WebSiteHelper.GetJson("document.getElementById(\"farfee\").innerHTML=" + this.GetFarfee(teamid, num, fee) + ";toal();", "eval"));
    }

}